/*******************************************************************************
 * Copyright (c) 2007 University of Oslo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University of Oslo - initial API and implementation
 *******************************************************************************/

package com.cea.papyrus.diagram.sequence.sedi.edit.command;

import org.eclipse.gef.commands.Command;

import com.cea.papyrus.diagram.sequence.sedi.model.util.ModelUtil;
import com.cea.papyrus.diagraminterchange2.di2.GraphEdge;

/**
 * Moves the anchor for a message.
 * <p>
 * The following attributes must be set before the command can run successfully:
 * </p>
 * <ul>
 * <li>Link
 * <li>Anchor
 * <li>Source
 * </ul>
 * <p>
 * When completed, the following attributes can be read to access the result:
 * </p>
 * <ul>
 * <li>None
 * </ul>
 * 
 * @author frankd@ifi.uio.no
 */
public class MoveAnchorCommand extends Command {
    private GraphEdge link;
    private String oldAnchor;
    private String anchor;
    private boolean source;

    public MoveAnchorCommand(final GraphEdge link, final String anchor, final boolean source) {
        this.link = link;
        this.anchor = anchor;
        this.source = source;
    }

    public void execute() {
        if (source) {
            oldAnchor = ModelUtil.getSourceRef(link);
            ModelUtil.setSourceRef(link, anchor);
        } else {
            oldAnchor = ModelUtil.getTargetRef(link);
            ModelUtil.setTargetRef(link, anchor);
        }
    }

    public void undo() {
        if (source)
            ModelUtil.setSourceRef(link, oldAnchor);
        else
            ModelUtil.setTargetRef(link, oldAnchor);
        oldAnchor = null;
    }

    @Override
    public void redo() {
        execute();
    }

    public void dispose() {
        link = null;
        oldAnchor = null;
        anchor = null;
    }
}
